#include "StdAfx.h"
#include "ResourceManager.h"
#include "Vector2Int.h"
#include "Prefab.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

ResourceManager* ResourceManager::ms_pInstance;

void ResourceManager::Init (const char* path)
{
	FILE* pFile = fopen(path, "r");
	if (pFile != NULL)
	{
		Prefab* prefab;
		int id;
		fscanf(pFile, "ID %d\n", &id);
		while (id >= 0)
		{
			prefab = new Prefab();
			prefab->m_iId = id;
			m_prebab.push_back(prefab);

			Vector2Int block;
			fscanf(pFile, "BLOCK (%d,%d)\n", &block.x, &block.y);
			while (block.x >= 0)
			{
				prefab->m_block.push_back(block);
				fscanf(pFile, "BLOCK (%d,%d)\n", &block.x, &block.y);
			}

			Vector2Int rotate_space;
			fscanf(pFile, "ROTATE_SPACE (%d,%d)\n", &rotate_space.x, &rotate_space.y);
			while (rotate_space.x >= 0)
			{
				prefab->m_spaceToRotate.push_back(rotate_space);
				fscanf(pFile, "ROTATE_SPACE (%d,%d)\n", &rotate_space.x, &rotate_space.y);
			}

			int rotatePrefabId;
			fscanf(pFile, "ROTATE_PREFAB %d\n", &rotatePrefabId);
			prefab->m_iRotatePrefabId = rotatePrefabId;

			fscanf(pFile, "ID %d\n", &id);
		}
		fclose(pFile); 
	}
	srand(time(NULL));
}

int ResourceManager::GetRandomPrefab ()
{
	return rand() % m_prebab.size();;
}

Prefab* ResourceManager::GetPrefab (int id)
{
	for (int i=0; i<m_prebab.size(); i++)
	{
		if (m_prebab[i]->m_iId == id)
			return m_prebab[i];
	}
	return NULL;
}
